/*
 * (C) Copyright 2013 George Daswani (georgedaswani@gmail.com) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *    George Daswani (georgedaswani@gmail.com)
 */

package am.api.model;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class AMString implements Serializable {

	private final static long serialVersionUID = 1L;

	public final static AMString create(int bufferLen) {
		return new AMString(bufferLen);
	}

	private byte[] buffer;

	private AMString(int bufferLen) {
		super();
		buffer = new byte[bufferLen];
	}

	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		AMString rhs = (AMString) obj;
		return new EqualsBuilder().appendSuper(super.equals(obj))
				.append(buffer, rhs.buffer).isEquals();
	}

	public byte[] getBuffer() {
		return buffer;
	}

	public int getBufferLength() {
		return buffer.length;
	}

	public int hashCode() {
		return new HashCodeBuilder(61, 27).append(buffer).toHashCode();
	}

	public void setBuffer(byte[] buffer) {
		this.buffer = buffer;
	}

	public String toString() {
		return new String(buffer);
	}

}
